<template>
  <div style="font-size: 14px;" id="home-main">

    <van-swipe :autoplay="3000" :height="207">
      <van-swipe-item v-for="(item,index) in banners" :key="index">
        <img v-lazy="require('../assets/img/'+item)" style="height: 207px;width: 100%;"/>
      </van-swipe-item>
    </van-swipe>

    <van-grid :border="false" :column-num="3">
      <van-grid-item v-for="(item,index) in types" :key="index">
        <div>
          <div>
            <van-image :src="require('../assets/img/'+item.img)" style="height: 60px;"/>
          </div>
          <div class="type-name">
            {{item.name}}
          </div>
        </div>
      </van-grid-item>
    </van-grid>

    <div style="background-color: #f5f5f5;padding-top: 10px;">
      <div class="title">
        推荐商品
      </div>
      <van-grid :border="false" :column-num="2" :gutter="10">
        <van-grid-item v-for="(item,index) in products" :key="index" @click="productDetail(item)">
          <div>
            <van-image class="big"  :src="require('../assets/img/'+item.img)" style="height: 200px;"/>
          </div>
          <div class="productName">
            {{item.productName}}
          </div>
          <div class="price">
            ¥{{item.price}}
          </div>
        </van-grid-item>
      </van-grid>
    </div>

  </div>
</template>

<script>
  import data from "@/data/data.json";
  import {
    Swipe,
    SwipeItem,
    Grid,
    GridItem,
    Image as VanImage
  } from 'vant';

  export default {

    components: {
      [Swipe.name]: Swipe,
      [SwipeItem.name]: SwipeItem,
      [Grid.name]: Grid,
      [GridItem.name]: GridItem,
      [VanImage.name]: VanImage
    },

    data() {
      return {
        banners:[],//轮播图
        topTypes:[//顶部分类
          {id:1,name:'推荐'},
          {id:2,name:'智能'},
          {id:3,name:'电视'},
          {id:4,name:'家电'},
          {id:5,name:'冰箱'},
          {id:6,name:'洗衣机'},
          {id:7,name:'空调'},
          {id:8,name:'手机'}
        ],
        types:[],//分类
        products:[]//商品列表
      };
    },

    mounted() {
      //布局状态控制
      this.$store.commit('SET_HEADER_FOOTER', {showIndexHeader: true,headerName:'首页',pageName:'address.vue',showFooter: true});
      this.banners = data.banners;
      this.types = data.types;
      this.products = data.products;
    },

    methods: {

      toType(){//跳转分类
        this.$router.push({'path': '/type'});
      },

      productDetail(item){//商品详情
        this.$router.push({
          path:'/productDetail',
          query:{
            id: item.id
          }
        })
      }
      
    }
  };
</script>

<style scoped>
  #home-main {
    margin: 0;
    position: relative;
    padding-bottom: 3.2rem;
  }
  .productName {
    font-size: 16px;
    font-weight: 600;
    margin: 10px 10px 5px 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .price {
    color: red;
    font-weight: 600;
    font-size: 18px;
  }
  .type-name{
    text-align: center;
    font-size: 16px;
  }
  .title{
    font-weight: 600;
    font-size: 18px;
    color: #666;
    line-height: 40px;
    margin-left: 10px;
  }
</style>
